<?php

/**
 * @author Sanches
 * @copyright 2008
 */

include 'config.php';


function Layer_Add ($img,$type)
{
	global $PARTICULA;
	
	$layer = @imagecreatefrompng($PARTICULA['DIR']['img'].$PARTICULA['MOD']['cat']['mini_layer_file']);
	$transp = imagecolorallocate($layer);
	$layer = ResizeIMG($layer,$type);
	
	
	$x = imagesx($img);
	$y = imagesy($img);
	
	$pat = imagecreatetruecolor($x, $y);
	imagecolortransparent($pat,$transp);
	
	imagecopymerge($pat,$img, 0, 0, 0, 0, $x, $y,100);
	
	imagecolortransparent($pat,$transp);
	
	imagecopymerge($pat,$layer, 0, 0, 0, 0, $x, $y,10);
	
	imagecolortransparent($pat,$transp);
	
	$img = $pat;
	
	return $img;
}

function ResizeIMG ($img,$type,$cat)
{	
	global $PARTICULA;
    
    if ($type == "m")
    {
    $nw = $PARTICULA['MOD']["cat$cat"]['img_width_mini'];
    }
    if ($type == "b")
    {
	$nw = $PARTICULA['MOD']["cat$cat"]['img_width_big'];
	}
    
    $transp = imagecolorallocate($img,0,0,0);
    
    $x = imagesx($img);
	$y = imagesy($img);
	if ($x > $nw)
	{
		$nx = $nw;
		$ny = round(($y*$nx)/$x);
		//echo $nx." - ".$ny;
		//die;
		$newimg = imagecreatetruecolor($nx, $ny);
		imagecolortransparent($newimg,$transp);
		imagecopyresampled ($newimg,$img,0,0,0,0,$nx,$ny,$x,$y);
		
	}
	else
	{
	$newimg=$img;
	imagecolortransparent($newimg,$transp);	
	}
    
    
    
    return $newimg;
}

if (isset($_REQUEST['file']))
{
//$_REQUEST['file']=basename($_REQUEST['file']);
$file = $PARTICULA['DIR']['files'].$_REQUEST['file'];
header('Content-Disposition: attachment; filename="'.$_REQUEST['file'].'"');
readfile($file);	
}

if (isset($_REQUEST['jpg']))
{
$_REQUEST['jpg']=basename($_REQUEST['jpg']);
$file = $PARTICULA['DIR']['img'].$_REQUEST['jpg'];
if (!file_exists($file))
{
$file = $PARTICULA['DIR']['tpl'].$_REQUEST['jpg'];	
}
//$img = imagecreatefromstring($data['data']);
$img = @imagecreatefromjpeg($file);
//$img = Layer_Add($img,$type);
$img = ResizeIMG($img,$_REQUEST['type'],$_REQUEST['cat']);

header("Content-type: image/jpeg");
header("Expires: ".date("r",time()+604800)); 
imagejpeg($img);		
}
else
if (isset($_REQUEST['png']))
{
$_REQUEST['png']=basename($_REQUEST['png']);
$file = $PARTICULA['DIR']['img'].$_REQUEST['png'];
if (!file_exists($file))
{
$file = $PARTICULA['DIR']['tpl'].$_REQUEST['png'];	
}
//$img = imagecreatefromstring($data['data']);
$img = @imagecreatefrompng($file);
$img = ResizeIMG($img,$_REQUEST['type'],$_REQUEST['cat']);
header("Content-type: image/png");
header("Expires: ".date("r",time()+604800)); 
imagepng($img);		
}
else
if (isset($_REQUEST['gif']))
{
$_REQUEST['gif']=basename($_REQUEST['gif']);
$file = $PARTICULA['DIR']['img'].$_REQUEST['gif'];
if (!file_exists($file))
{
$file = $PARTICULA['DIR']['tpl'].$_REQUEST['gif'];	
}
//$img = imagecreatefromstring($data['data']);
$img = @imagecreatefromgif($file);
$img = ResizeIMG($img,$_REQUEST['type'],$_REQUEST['cat']);
header("Content-type: image/gif");
header("Expires: ".date("r",time()+604800)); 
imagegif($img);		
}


?>